package com.bidevalution.controller.task;

import com.bidevalution.business.service.BeDataSyncService;
import com.bidevalution.business.service.BeSectionEnterpriseService;
import com.bidevalution.dao.entity.*;
import com.bidevalution.dao.mapper.BeSectionInfoMapper;
import com.google.common.collect.Maps;
import com.qtp.core.common.RemoteService;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.GsonUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by glodon on 2019-08-28.
 */
@Component
public class BidFileInfoTask {
    @Value("${ids.url}")
    private String ptIdsUrl;

    @Autowired(required = false)
    private BeSectionInfoMapper beSectionInfoMapper;

    @Autowired(required = false)
    private BeSectionEnterpriseService beSectionEnterpriseService;

    @Autowired
    private BeDataSyncService beDataSyncService;
    @Value("${upload.file.path}")
    private String uploadFilePath;

    private Logger log = Logger.getLogger(getClass());

    /**
     * 10分钟执行一次
     *
     * @throws Exception
     */
    //@Scheduled(cron = "0 */10 *  * * * ")
    public void syncCandidateInfoData() {
        try {
            List<BeSectionInfo> beSectionInfos = beSectionInfoMapper.getTodaySectionList();
            for (BeSectionInfo beSectionInfo : beSectionInfos) {
                if (beSectionInfo != null && beSectionInfo.getBidOpenTime().getTime() > DateUtil.getCurrentTime()) {
                    Map<String, Object> params = Maps.newHashMap();
                    params.put("bsID", beSectionInfo.getSeqId());
                    params.put("busiType", "1");
                    String bidViewUrl = ptIdsUrl + "/bidViewWz.shtml";
                    //调用t9平台接口查询投标单位信息
                    String response = RemoteService.getRemoteInfo(bidViewUrl, params);
                    BeSectionEnterprise beSectionEnterprise = null;
                    List<BeSectionEnterprise> beSectionEnterpriseList = new ArrayList<>();
                    if (StringUtils.isNotBlank(response)) {
                        Map<String, Object> resultMap = GsonUtil.jsonToMapWithGson(response);
                        if (resultMap != null) {
                            List<Map<String, Object>> bidList = (List<Map<String, Object>>) resultMap.get("bids");
                            if (bidList != null && bidList.size() > 0) {
                                for (Map<String, Object> map : bidList) {
                                    if (map != null) {
                                        beSectionEnterprise = new BeSectionEnterprise();
                                        beSectionEnterprise.setSectionId(beSectionInfo.getSeqId());
                                        beSectionEnterprise.setSeqId((String) map.get("orgGUID"));
                                        beSectionEnterprise.setPtEnterpriseId((String) map.get("orgEnterId"));
                                        beSectionEnterprise.setEnterpriseName((String) map.get("orgName"));
                                        beSectionEnterprise.setCreditCode((String) map.get("orgCode"));
                                        beSectionEnterprise.setPayDepositAccount((String) map.get("payBascAccno"));
                                        beSectionEnterprise.setPayDepositType((String) map.get("payAccnoIsSame"));
                                        beSectionEnterprise.setPayDeposit(((Long) map.get("payedBail")).intValue());
                                        beSectionEnterprise.setBidfileId((String) map.get("fileID"));
                                        beSectionEnterprise.setBidfileName((String) map.get("fileName"));
                                        beSectionEnterprise.setSubmitBidfileTime(DateUtil.stringToDate((String) map.get("fileUploadTime"), DateUtil.PT2));
                                        String bidHideNumber = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
                                        beSectionEnterprise.setBidHideNumber(bidHideNumber);
                                        beSectionEnterpriseList.add(beSectionEnterprise);
                                    }
                                }
                                //同步投标单位信息
                                beDataSyncService.syncEnterpriseInfo(beSectionEnterpriseList);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 10分钟执行一次
     *
     * @throws Exception
     */
    //@Scheduled(cron = "0 */10 *  * * * ")
//    @Scheduled(cron = "0 34 16 * * ? ")
    public void syncBidFileInfoData() {
        try {
            /*long startTime = System.currentTimeMillis();
            System.out.println("*****开始导入投标文件，当前时间：*****"+DateUtil.sqlTimestamp());
            long overTime = startTime+(60*1000);
            for(int i=1;startTime<overTime;i++) {*/
                List<BeSectionEnterprise> beSectionEnterpriseList = beSectionEnterpriseService.getImportEnterpriseList();
                if (beSectionEnterpriseList !=null) {
                    for (BeSectionEnterprise beSectionEnterprise : beSectionEnterpriseList) {
                        beDataSyncService.syncBidFileInfo(beSectionEnterprise);
                    }
                }
                /*long time = System.currentTimeMillis() - startTime;
                BigDecimal b1 = BigDecimal.valueOf(time);
                BigDecimal b2 = BigDecimal.valueOf(1000);
                double userTime = b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP).doubleValue();
                System.out.println("*****第"+i+"次导入投标文件用时（s）*****" + userTime);
                startTime = System.currentTimeMillis();
            }
            System.out.println("*****结束导入投标文件，当前时间：*****"+DateUtil.sqlTimestamp());*/
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public static void main(String[] args) {
        BidFileInfoTask task = new BidFileInfoTask();
        task.syncBidFileInfoData();
    }
}
